package com.car.advertising.view;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

import com.car.advertising.R;
import com.car.advertising.view.viewAdapter.BottomMenuAdapter;
import com.car.advertising.view.viewAdapter.CustomLinearLayoutManager;
import com.support.widget.interfacelistener.OnRecyclerViewItemClickListener;

import java.util.ArrayList;
import java.util.List;

public class BottomMenu implements OnTouchListener, OnRecyclerViewItemClickListener {

    private PopupWindow popupWindow;
    private View mMenuView;
    private Activity mContext;
    private RecyclerView carOwnerGv;
    private List<String> cityList = new ArrayList<>();
    private CityAbbreviationListener abbreviationListener;

    public BottomMenu(Activity context, CityAbbreviationListener abbreviationListener) {
        LayoutInflater inflater = LayoutInflater.from(context);
        this.abbreviationListener = abbreviationListener;
        mContext = context;
        mMenuView = inflater.inflate(R.layout.layout_popwindow, null);
        carOwnerGv = (RecyclerView) mMenuView.findViewById(R.id.car_owner_rv);

        String[] array = context.getResources().getStringArray(R.array.abbreviation);
        for (String cityStr : array) {
            cityList.add(cityStr);
        }

        BottomMenuAdapter menuAdapter = new BottomMenuAdapter(context, cityList);
        CustomLinearLayoutManager layoutManager = new CustomLinearLayoutManager(context, 9);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager.setScrollEnabled(false);
        carOwnerGv.setLayoutManager(layoutManager);
        carOwnerGv.setAdapter(menuAdapter);
        menuAdapter.setOnItemClickListener(this);

        popupWindow = new PopupWindow(mMenuView, 500, 500, true);
        popupWindow.setWidth(LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
        popupWindow.setAnimationStyle(R.style.popwin_anim_style);
        ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.translate));
        popupWindow.setBackgroundDrawable(dw);
        mMenuView.setOnTouchListener(this);
    }

    /**
     * 显示菜单
     */
    public void show() {
        //得到当前activity的rootView  
        View rootView = ((ViewGroup) mContext.findViewById(android.R.id.content)).getChildAt(0);
        popupWindow.showAtLocation(rootView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    @Override
    public void onItemClick(ViewGroup parent, View view, int position) {
        abbreviationListener.onCityAbb(cityList.get(position));
        popupWindow.dismiss();
    }

    @Override
    public boolean onTouch(View arg0, MotionEvent event) {
        int height = mMenuView.findViewById(R.id.pop_layout).getTop();
        int y = (int) event.getY();
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (y < height) {
                popupWindow.dismiss();
            }
        }
        return true;
    }

    public interface CityAbbreviationListener {
        void onCityAbb(String abbreviation);
    }
}